package ywh.model
{
	import fl.events.SliderEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import ywh.event.CoreEvent;
	import ywh.event.SyncEvent;
	import ywh.manager.Core;
	import ywh.manager.CoreState;
	import ywh.manager.Sync;
	import ywh.player.av.AudioManager;
	import ywh.player.av.IAudioHandle;
	import ywh.player.media.OSMFPlayer;
	import ywh.player.media.StageVideoPlayer;
	import ywh.player.RelayoutSprite;
	import ywh.pool.PlayerPool;
	import ywh.ui.courseslider.CourseSlider2;
	import ywh.utils.TimeFormat;
	
	/**
	 * 视频播放器;
	 * @author 杨伟华
	 */
	public class Model2PlayCtrl extends RelayoutSprite
	{
		public var muteBtn:MovieClip;
		public var pauseBtn:MovieClip;
		public var playBtn:MovieClip;
		public var layoutBtn:MovieClip;
		public var timeTxt:TextField;
		public var courseSlider:CourseSlider2;
		public var bg:Sprite;
		
		private var sliderIsDraging:Boolean = false;
		private var am:AudioManager;
		
		public function Model2PlayCtrl()
		{
		
		}
		
		internal function startSync():void
		{
			initShow();
			initEvent();
		}
		
		private function initShow():void
		{
			playBtn.gotoAndStop("off");
			pauseBtn.gotoAndStop("off");
			muteBtn.gotoAndStop("on");
			removeChild(playBtn);
			timeTxt.text = "00:00:00 / 00:00:00";
			timeTxt.mouseEnabled = false;
			timeTxt.multiline = false;
		}
		
		private function initEvent():void
		{
			playBtn.buttonMode = true;
			pauseBtn.buttonMode = true;
			muteBtn.buttonMode = true;
			layoutBtn.buttonMode = true;
			this.addEventListener(MouseEvent.MOUSE_DOWN, this_mouseDown);
			
			courseSlider.addEventListener(SliderEvent.THUMB_PRESS, toggleDragging);
			Core.addEventListener(SyncEvent.SYNC_LIST, timeChangeHandle);
			Core.addEventListener(CoreEvent.CORE_STATE_CHANGE, coreStateChangeHandle);
			am = new AudioManager(PlayerPool.getPlayerById("f4v") as IAudioHandle);
		}
		
		private function this_mouseDown(e:MouseEvent):void
		{
			switch(e.target) {
				case playBtn: 
					Sync.play();
					break;
				case pauseBtn: 
					Sync.pause();
					break;					
				case muteBtn:
					var isMute:Boolean = am.isMute;
					isMute ? am.unmute() : am.mute();
					muteBtn.gotoAndStop(isMute ? "on" : "off");
					break;
				case layoutBtn:
					(parent as Model2).toggleLayout();
					break;
			}
		}
		
		private function coreStateChangeHandle(e:CoreEvent):void
		{
			switch (Core.state)
			{
				case CoreState.CORE_PLAYING: 
					playBtn.parent && removeChild(playBtn);
					pauseBtn.mouseEnabled = true;
					addChild(pauseBtn);
					break;
				case CoreState.CORE_PAUSED: 
					pauseBtn.parent && removeChild(pauseBtn);
					playBtn.mouseEnabled = true;
					addChild(playBtn);
					break;
				default: 
					playBtn.mouseEnabled = false;
					pauseBtn.mouseEnabled = false;
					break;
			}
		}
		
		private function timeChangeHandle(e:SyncEvent):void
		{
			if (!sliderIsDraging)
			{
				courseSlider.value = e.time / Sync.duration * courseSlider.maximum;
			}
			timeTxt.text = TimeFormat.ss2hhmmss(e.time) + " / " + TimeFormat.ss2hhmmss(Sync.duration);
		}
		
		private function toggleDragging(e:SliderEvent):void
		{
			if (sliderIsDraging = (e.type == SliderEvent.THUMB_PRESS))
			{
				courseSlider.addEventListener(SliderEvent.THUMB_RELEASE, toggleDragging);
			}
			else
			{
				courseSlider.removeEventListener(SliderEvent.THUMB_RELEASE, toggleDragging);
				var time:Number = (e.value / courseSlider.maximum) * Sync.duration;
				Sync.time = time;
			}
		}
		
		override protected function handleSetRectangle(rec:Rectangle):void
		{
			super.handleSetRectangle(rec);
			horizantalLayout(rec.width, 5, [10, playBtn, 20], [20, muteBtn, layoutBtn, 10], courseSlider);
			pauseBtn.x = playBtn.x;
			bg.width = rec.width;
		}
	}

}